<?php

use Biz\module\Packages\Pay\PayAbstruct;
use Biz\service\course\Course as sCourse;
use Biz\service\course\CourseOrder as sCourseOrder;
use Biz\service\trade\Pay;
use Doraemon\tools\HhzClient;

class ConfirmOrderAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REALNAME;
    const PAY_TIPS_ANDROID = "1、课程为虚拟商品，购买后不支持退款、退换，请确认后购买
2、课程购买后可无限次观看";
    const PAY_TIPS_IOS = "1、你将使用好好住虚拟货币「瓦片」进行支付
2、课程为虚拟商品，购买后不支持退款、退换，请确认后购买
3、课程购买后可无限次观看";

    public function main()
    {
        $courseId = $this->getRequest()->getPost('course_id', 0);
        if ($courseId && $this->uid) {
            $courseInfo = sCourse::getDetail($courseId);
            if (isset($courseInfo['is_free']) && $courseInfo['is_free'] && version_compare($this->appVersion, '4.19.0', "<")) {
                throw new \Exception('当前版本过低，请前往应用市场升级后免费观看课程');
            }
            $orderNo = sCourseOrder::createOrderNo($courseId, $this->uid);
            if (empty($orderNo)) {
                throw new \Exception('当前课程无法购买');
            }
        } else {
            throw  new \Exception('参数错误');
        }
        if (HhzClient::getClientType() == HhzClient::CLIENT_IOS) {
            $payWays = Pay::getPayWays(PayAbstruct::_IOS_COURSE_PAY_WAYS_LIST, $this->uid);
            $payTips = self::PAY_TIPS_IOS;
        } elseif (HhzClient::getClientType() == HhzClient::CLIENT_ANDROID) {
            $payWays = Pay::getPayWays(PayAbstruct::_ANDROID_COURSE_PAY_WAYS_LIST);
            $payTips = self::PAY_TIPS_ANDROID;
        } else {
            throw  new \Exception('参数错误');
        }
        $data = [
            'order_no'    => $orderNo,
            'summary'     => [
                'total_amount' => $courseInfo['price'],
                'total_buynum' => 1
            ],
            'course_list' => [
                [
                    'course_id'   => $courseInfo['course_id'],
                    'title'       => $courseInfo['title'],
                    'spec_img'    => $courseInfo['spec_img'],
                    'teacher_img' => $courseInfo['teacher_img'],
                    'price'       => $courseInfo['price']
                ]
            ],
            'pay_ways'    => $payWays,
            'pay_tips'    => $payTips
        ];
        $this->setSuccess($data);
    }
}